﻿using DevExpress.XtraEditors;
using DXApplicationTangche.DTO;
using DXApplicationTangche.Utils.UC;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DXApplicationTangche.UC.纳客
{
    public partial class Form纳客预览 : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        private SaleOrderFacadeDTO saleOrderFacadeDTO = new SaleOrderFacadeDTO();
        private bool push;
        public Form纳客预览(SaleOrderFacadeDTO saleOrderFacadeDTO, bool push)
        {
            InitializeComponent();
            this.push = push;
            this.barButtonItem1.Enabled = !push;
            this.refreshata(saleOrderFacadeDTO);
        }
        public void refreshata(SaleOrderFacadeDTO saleOrderFacadeDTO)
        {
            this.saleOrderFacadeDTO = saleOrderFacadeDTO;
            this.textBox纳客单号.Text = this.saleOrderFacadeDTO.billNo;
            this.textBox整单优惠.Text = this.saleOrderFacadeDTO.discount.ToString();
            this.textBox创建时间.Text = this.saleOrderFacadeDTO.createDate.ToString();
            this.textBox收款单单号.Text = this.saleOrderFacadeDTO.paymentOrderId;
            this.textBox销售订单单号.Text = this.saleOrderFacadeDTO.saleOrderId;
            this.textBox销售出库单单号.Text = this.saleOrderFacadeDTO.stockOutOrderId;
            this.gridControl预览.DataSource = this.saleOrderFacadeDTO.saleOrderFacadeBillEntryDTOS; 
            this.gridView预览.RefreshData();
        }

        private void 修改ToolStripMenuItem_Click(object sender, EventArgs e)
        {

            String entryId = this.gridView预览.GetRowCellValue(this.gridView预览.FocusedRowHandle, "entryId").ToString();
            foreach (SaleOrderFacadeBillEntryDTO item in this.saleOrderFacadeDTO.saleOrderFacadeBillEntryDTOS)
            {
                if (entryId.Equals(item.entryId))
                {
                    //    打开单条预览
                    Form纳客修改折扣 Form纳客修改折扣 = new Form纳客修改折扣(
                        item,
                        this.saleOrderFacadeDTO.discount,
                        this
                        );
                    Form纳客修改折扣.ShowDialog();
                    break;
                }
            }
        }

        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (this.saleOrderFacadeDTO.discount > 0 || this.saleOrderFacadeDTO.discount < 0)
            {
                MessageBox.Show("整单折扣不为0，禁止下推");
            }
            else {
                this.saleOrderFacadeDTO =
                    nakeService.pushSalOrder(this.saleOrderFacadeDTO.id);
                this.refreshata(saleOrderFacadeDTO);
                MessageBox.Show("下推成功");
            }
            this.barButtonItem1.Enabled = false;


        }
    }
}